package newui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GlideUtil;
import com.shwread.android.qysw10000243.R;

import java.util.List;

import logic.bean.BannerInfo;
import logic.constants.DefaultConsts;

/**
 * Created by lex on 2017/7/6.
 */

public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.TrainListHolder> implements View.OnClickListener {

    private Context context;
    private List<BannerInfo> list;

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener){
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public TrainAdapter(Context context, List<BannerInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public TrainListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(
                context).inflate(R.layout.item_train_list, parent,
                false);
        TrainListHolder holder = new TrainListHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(TrainListHolder holder, int position) {

        holder.tVItemTrainTitle.setText(list.get(position).getBannerName());
        holder.tVItemTrainDesc.setText("水电费可视对讲");
        Log.e("拼接的url图片",appendBannerUrl(list.get(position).getImgUrl()));
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.ivItemTrainIcon.getLayoutParams();
        lp.width = DefaultConsts.BANNER_DEFAULT_WIDTH;
        lp.height = DefaultConsts.BANNER_DEFAULT_HIGHT;
        holder.ivItemTrainIcon.setLayoutParams(lp);
        GlideUtil.load(context,appendBannerUrl(list.get(position).getImgUrl()),holder.ivItemTrainIcon);
        holder.itemView.setTag(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
        onRecyclerViewItemClickListener.onItemClick(view,view.getTag());
    }

    class TrainListHolder extends RecyclerView.ViewHolder {

        private TextView tVItemTrainTitle,tVItemTrainDesc;
        private ImageView ivItemTrainIcon;

        public TrainListHolder(View view) {
            super(view);
            tVItemTrainTitle = (TextView) view.findViewById(R.id.tVItemTrainTitle);
            tVItemTrainDesc = (TextView) view.findViewById(R.id.tVItemTrainDesc);
            ivItemTrainIcon = (ImageView) view.findViewById(R.id.ivItemTrainIcon);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Object data);
    }

    private String appendBannerUrl(String imgUrl){
        StringBuffer sb = new StringBuffer(imgUrl);
        sb.append("?c=0&w=");
        sb.append(DefaultConsts.BANNER_DEFAULT_WIDTH);
        sb.append("&h=");
        sb.append(DefaultConsts.BANNER_DEFAULT_HIGHT);
        sb.append("&p=0");
        return sb.toString();
    }
}
